package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

class WineRegionController {

    // REST operations
    // GET
    def show = {
        println 'WineRegionController >> Show >> START'
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        if (params.id && WineRegion.exists(params.id)) {    
            def wr = wrman.getWineRegionByID(params.id)
            render wr as JSON
            
            println 'Show wine type by ID >> ' + wr.id + " " + wr
        }
        else {
            def allWR = wrman.listWineRegions()
            if (allWR.length() > 0) {
                render allWR as JSON
            }
            else {
                JSONObject o = new JSONObject()
                o.put("404",true)
                render o as JSON
            }   
            
            println 'Listing all wine regions >> ' + allWR
        }
        
        println 'WineRegionController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'WineRegionController >> Update >> START'
        
        JSONObject o = request.JSON
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        CountryManagement cman = new CountryManagement()
        def wr = wrman.getWineRegionByID(o.get("id"))
        if (o.has("regionName")) { wr.regionName = o.get("regionName"); println wr.regionName }
        if (o.has("description")) { wr.description = o.get("description"); println wr.description }
        if (o.has("country")) {
            def cty = cman.findByCountryName(o.get("country"));
            if (cty == null) {
                println 'WineRegionController >> Save >> Country not found.'
            }
            else {
                wr.countryId = cty
            }
        }  
        
        def result = wrman.updateWineRegion(wr)
        if (result == true) {
            println 'WineRegionController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'WineRegionController >> Update >> ' + result
        }

        println 'WineRegionController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'WineRegionController >> delete >> START'
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        if (params.id && WineRegion.exists(params.id)) {    
            def result = wrman.deleteWineRegion(id)
            
            if (result == true) {
            println 'WineRegionController >> delete >> ' + result
    //            render response as JSON
            }
            else {
                println 'WineRegionController >> delete >> ' + result
            }
        }
        
        println 'WineRegionController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'WineRegionController >> Save >> START'
        
        JSONObject o = request.JSON
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        def result = wrman.addWineRegion(o)
        if (result == true) {
            println 'WineRegionController >> Save >> ' + result
//            render response as JSON
        }
        else {
            println 'WineRegionController >> Save >> ' + result
        }
        
        println 'WineRegionController >> Save >> END'
    }
}
